#include <SDL.h>
#include <SDL_ttf.h>
#include <string>
#include "LOK2ChatInput.h"
#include "LOK2ChatDisplay.h"
#include "LOK2WindowManager.h"
#include "SDLutil.h"
#include "SDLClipboard.h"
#include "SDLPopupMenu.h"
#include "SDLHScrollBar.h"
#include "defines.h"

#pragma warning(disable: 4786)

SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Event event;
TTF_Font *font = NULL;
SDL_Color textColor = {0xFF, 0xFF, 0x00};

SDLClipboard *pCB = NULL;
//SDLPopupMenu *pPop = NULL;


using namespace std;

int main(int argc, char* args[]) {
	bool done = false;
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1) return 1;
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_SRCALPHA);
    if(screen == NULL) return 1;
	if(TTF_Init() == -1) return 1;
    SDL_WM_SetCaption("Super Secret LOK Client v0.10a", NULL);
    background = load_image("010.dat");
	font = TTF_OpenFont("arial.ttf", 14);
    apply_surface(0, 0, background, screen);
    if(SDL_Flip(screen) == -1) return 1;
	if(!screen) return 1;
	if(!font) return 1;
	//if(!textColor) return 1;
	pCB = new SDLClipboard();
	//SDLTextBox *myTextBox = new SDLTextBox(screen, font, textColor, &event, pCB, NULL);
	LOK2ChatDisplay *myChatDisplay = new LOK2ChatDisplay(screen, font, &event, pCB, 512);
	LOK2ChatMessageHandler *myChatMsgHandler = new LOK2ChatMessageHandler(myChatDisplay);
	LOK2ChatInput *myChatInput = new LOK2ChatInput(screen, font, textColor, &event, pCB, myChatMsgHandler, "textbox.bmp");
	LOK2WindowManager *myWndManager = new LOK2WindowManager(&event);
	SDL_Rect dstRect = { 370, 0, 20, 460 };
	SDL_Rect upButtonRect = { 0, 30, 20, 20 };
	SDL_Rect downButtonRect = { 0, 70, 20, 20 };
	SDL_Rect bgRect = { 20, 0, 20, 460 };
	SDL_Rect dummy = {0, 0, 0, 0};
	SDLHScrollBar *myHScrollBar = new SDLHScrollBar(0, dstRect, upButtonRect, downButtonRect, bgRect, dummy, dummy, dummy, "hscroll.bmp", myChatDisplay, screen, &event);
	myWndManager->addWindow(myChatInput);
	myWndManager->addWindow(myChatDisplay);
	while(!done) {
		SDL_Delay(100);
		while(SDL_PollEvent(&event)) {
			myWndManager->manage();
			if(event.type == SDL_QUIT) done = true;
		}
		apply_surface(0, 0, background, screen);
		myChatDisplay->display();
		myHScrollBar->display();
		myChatInput->display();
		if(SDL_Flip(screen) == -1) return 1;
	}
	delete myChatInput;
	delete myChatDisplay;
	delete myWndManager;
    SDL_FreeSurface(background);
	TTF_CloseFont(font);
	TTF_Quit();
    SDL_Quit();
    return 0;
}
